import Vue from 'vue';
import VueRouter from 'vue-router';
import index from '../views/index';
import envShow from '../components/common/envShow' ;
import reqShow from '../components/common/reqShow' ;
import Home from '../views/home_old' ;

Vue.use(VueRouter);

// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
// 修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location)
    .catch(err => err);
};

const routes = [
  {
    // 重定向，用来指向一打开网页就跳转到哪个路由
    path: '/',
    redirect: '/home',
  },
  {
    // 首页
    path: '/home',
    name: 'home',
    component: Home,
    meta: { title: 'home页' },
    children: [{
      path: '/index1',
      name: 'index1',
      component: index,
      meta: { title: 'index页' },
    },
      {
        path: '/env/:id',
        name: 'env',
        component: envShow,
      },
      {
        path: '/req/:id',
        name: 'req',
        component: reqShow,
      },
    ]
  },
  {
    // 首页
    path: '/index',
    name: 'index',
    component: index,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/error/404.vue'),
  },
  // 注意，为了设置404页面，一定要放在最后面，这样当服务器找不到页面的时候就会返回404
  {
    path: '*',
    redirect: '/404'
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

export const createRouter = () => new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});
export default router;
